package org.nearby.note.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.nearby.note.bean.user;
import org.nearby.note.utils.*;

public class userDaoImpl implements userDao {
	private Connection conn;
	private Statement smt;
	private PreparedStatement psmt;
	private ResultSet rs;
	private int affectedRows;

	protected static int uid = 0; // 验证是否登录的
	protected static user userLogin = new user();

	public static boolean checkIsLogin() {
		return (uid > 0);
	}

	public static int getUid() {
		return uid;
	}

	public static user getUserLogin() {
		return userLogin;
	}

	public  void deleteSession() {
		conn = DbUtil.getsqliteconnect(false);
		try {
			psmt = conn.prepareStatement("delete from session where user_id = ?");

			psmt.setInt(1, getUid());
			psmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 检测用户登录
	 * 
	 * @return 0 用户名或密码错误 >0登录成功 -1表示已经登录过
	 */
	public int checkUser(user u) {
		conn = DbUtil.getsqliteconnect(false);
		try {
			smt = conn.createStatement();

			rs = smt.executeQuery("select * from user where user_name = '"
					+ u.getUserName() + "' and user_pwd = '" + u.getUserPwd()
					+ "';");
			if (rs.next()) {
				int user_id = rs.getInt("user_id");
				psmt = conn
						.prepareStatement("select * from session where user_id = ? and login_datetime > ?");
				psmt.setInt(1, user_id);
				psmt.setString(
						2,
						String.valueOf(Long.valueOf(common.time())
								- Long.valueOf(props.get("session_timeout"))));

				rs = psmt.executeQuery();
				if (rs.next()) {
					return -1;
				}
				userLogin.setUserName(u.getUserName());
				return (uid = user_id);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public boolean recordLoginStatus(int user_id) {
		conn = DbUtil.getsqliteconnect(false);
		try {
			psmt = conn
					.prepareStatement("select * from session where  user_id = ?");
			psmt.setInt(1, user_id);
			rs = psmt.executeQuery();
			if (rs.next()) {
				psmt = conn
						.prepareStatement("update session set login_datetime = ? where user_id = ?");
				psmt.setString(1, common.time());
				psmt.setInt(2, user_id);
				affectedRows = psmt.executeUpdate();
			} else {
				String sql = "insert into session(login_datetime, user_id) values(?,?)";
				psmt = conn.prepareStatement(sql);
				psmt.setString(1, common.time());
				psmt.setInt(2, user_id);
				affectedRows = psmt.executeUpdate();
			}
			return (affectedRows == 0);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close();
		}
		return false;
	}

	public void close() {
		try {
			if (smt != null) {
				smt.close();
			}
			if (psmt != null) {
				psmt.close();
			}
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public int regUser(user u) {
		conn = DbUtil.getsqliteconnect(false);
		if (u.getUserName().equals("") || u.getUserPwd().equals("")) {
			return -2;
		}
		try {
			psmt = conn
					.prepareStatement("select * from user where user_name = ?");
			psmt.setString(1, u.getUserName());
			rs = psmt.executeQuery();
			if (rs.next()) {
				return -1;
			}
			psmt = conn
					.prepareStatement("insert into user (user_name, user_pwd) values (?, ?)");
			psmt.setString(1, u.getUserName());
			psmt.setString(2, u.getUserPwd());
			psmt.executeUpdate();

			rs = psmt.getGeneratedKeys();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close();
		}
		return 0;
	}
}
